#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
from os.path import join as pjoin
import sys,re,urllib2,urllib,time,xlwt,os,json,codecs,csv,smtplib,io

type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')

class Youtube_video:
    #开始方法
    def __init__(self):
        self.arr = []

    def start(self):
        print 1
        today_time = time.strftime("%m_%d")
        yesterday_time = time.strftime("%m_%d")

        now = int(time.time()) 
        timeStamp = now - 24*60*60
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%m_%d", timeArray)

        yesterday_fr = open(pjoin('./', 'ytb_%s.json'%otherStyleTime))
        yesterday_model = (json.load(yesterday_fr))

        today_fr = open(pjoin('./', 'ytb_%s.json'%today_time))
        today_model = (json.load(today_fr))
        self.model(yesterday_model,today_model)

   

    def model(self,yesterday_model,today_model):
        for title in today_model:
            try:
                self.wipeoff(yesterday_model[title],today_model[title],title)
            except:
                self.wipeoff('yesterday_model[title]',today_model[title],title)


    def wipeoff(self,y,t,title):
        today_now =[]
        # print title
        # print video_id
        # print int(t['subscription'])
        # print int(y['subscription'])

        djson = {
            "%s"%(title):{
            }
        }

        
        for tt in t:
            try: 
                djson["%s"%(title)]["%s"%tt] = {
                    "title":t[tt]['title'],
                    "publishedAt":t[tt]['publishedAt'],
                    "viewCount":str(int(t[tt]['viewCount']) - int(y[tt]['viewCount'])),
                    "commentCount": str(int(t[tt]['commentCount']) - int(y[tt]['commentCount'])),
                    "dLikeCount":str(int(t[tt]['dLikeCount']) - int(y[tt]['dLikeCount']))
                }
            except:
                try:
                    djson["%s"%(title)]["%s"%tt] = {
                        "title":t[tt]['title'],
                        "publishedAt":t[tt]['publishedAt'],
                        "viewCount":t[tt]['viewCount'],
                        "commentCount": t[tt]['commentCount'],
                        "dLikeCount":t[tt]['dLikeCount']
                    }
                except:        
                    print tt

        try:
            djson["%s"%(title)]['subscription'] = int(t['subscription']) - int(y['subscription'])
        except:        
            djson["%s"%(title)]['subscription'] = int(t['subscription'])

        self.d_json(djson)

    def d_json(self,data):
        month_day = time.strftime("%m_%d")
        # 写入数据
        if os.path.exists('./ytb_%s.csv'%(month_day)) == False:
            with codecs.open('./ytb_%s.csv'%(month_day), 'w', 'utf_8_sig') as f:
                f.write('标题,项目,%s'%(month_day))
        
        for title in data:
            with codecs.open('./ytb_%s.csv'%(month_day), 'a', 'utf_8') as f:
                f.write('\n\n%s,订阅数,%s\n'%(title,data[title]['subscription']))
            for dd in data[title]:
                with codecs.open('./ytb_%s.csv'%(month_day), 'a', 'utf_8') as ff:
                    try:
                        ff.write('\r%s,发布时间,%s\n,观看次数,%s\n,留言数,%s\n,表态,%s\n'%(data[title][dd]['title'],data[title][dd]['publishedAt'],data[title][dd]['viewCount'],data[title][dd]['dLikeCount'],data[title][dd]['commentCount']))
                    except:
                        print u"不存在"



spider = Youtube_video()
spider.start()

